package com.sumover.controller;

import com.sumover.POJO.User;
import com.sumover.POJO.UserDetail;
import com.sumover.Service.UserService;
import com.sumover.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.nio.cs.US_ASCII;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * 基础的用户操作.
 * 登录, 注册, 查看页面等.
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 用户登录
     *
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public ResultVo login(@RequestBody User user, HttpSession session) {
        System.out.println("user login");
        user = userService.checkUserExist(user);
        if (user == null) {
            return ResultVo.error("user not exist!");
        }
        session.setAttribute("loginUser", user);
        return ResultVo.success("success!", user);
    }


    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @RequestMapping("/signUp")
    public ResultVo signUp(@RequestBody User user) {
        String username = user.getUsername(), password = user.getPassword();
        if (userService.checkUserExist(username, password)) {
            return ResultVo.error("userExist");
        }
        userService.addNewUser(username, password);
        return ResultVo.success("userAddSuccess", null);
    }

    /**
     * 给管理员开放的接口
     * 查询所有的用户
     *
     * @param session
     * @return
     */
    @RequestMapping("/queryAllUser")
    public ResultVo queryAllUser(HttpSession session) {
        List<User> resultUserList = userService.queryAllUser();
        return ResultVo.success("query user success", resultUserList);
    }


    /**
     * 修改用户名
     *
     * @param updateName
     * @param session
     * @return
     */
    @RequestMapping("/updateUsername")
    public ResultVo updateUsername(@RequestBody String updateName, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            return ResultVo.error("userNotLogin");
        } else if (userService.checkUsernameRepeat(updateName)) {
            return ResultVo.error("username repeat");
        } else {
            userService.updateUserName(user, updateName);
            session.setAttribute("loginUser", user);
            return ResultVo.success("update success!", user);
        }
    }

    /**
     * 更新密码
     *
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/updatePassword")
    public ResultVo updatePassword(@RequestBody String password, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            return ResultVo.error("user not login!");
        } else {
            user.setPassword(password);
            userService.updateUserPassword(user, password);
            session.setAttribute("loginUser", user);
            return ResultVo.success("password update success", user);
        }
    }


    /**
     * 获得用户详情
     *
     * @param queryUser
     * @param session
     * @return
     */
    @RequestMapping("/getUserDetail")
    public ResultVo getUserDetail(@RequestBody User queryUser, HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResultVo.error("user not login!");
        }
        UserDetail userDetail = userService.getUserDetail(queryUser);
        if (userDetail == null) {
            //TODO 在userService 中添加一个添加userDetail实例
            return ResultVo.success("user detail returned, which was created", userDetail);
        }
        return ResultVo.success("user detail returned", userDetail);
    }

    /**
     * 更新用户详情
     *
     * @param userDetail
     * @param session
     * @return
     */
    @RequestMapping("/updateUserDetail")
    public ResultVo updateUserDetail(@RequestBody UserDetail userDetail, HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResultVo.error("user not login!");
        }
        userService.updateUserDetail(userDetail);
        return ResultVo.success("user detail update success!", null);
    }
}
